from datetime import datetime, timezone
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Boolean
from sqlalchemy.orm import relationship
from app.db.db import Base


class DUser(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(100), unique=True, index=True, nullable=False)
    email = Column(String(100), unique=True, index=True, nullable=False)
    hashed_password = Column(String(128), nullable=False)
    created_at = Column(DateTime, default=datetime.now(timezone.utc))
    updated_at = Column(
        DateTime,
        default=datetime.now(timezone.utc),
        onupdate=datetime.now(timezone.utc),
    )
    is_active = Column(Boolean, default=True)
    is_admin = Column(Boolean, default=False)

    accounts = relationship("DAccount", lazy="dynamic")
    # def __repr__(self):
    #     return f"<User(id={self.id}, username={self.username}, email={self.email})>"


class DAccount(Base):
    __tablename__ = "accounts"
    id = Column(Integer, primary_key=True, index=True)
    owner_id = Column(Integer, ForeignKey("users.id"))

    ########## 账号本身的信息 #########
    account_username = Column(String(100), index=True)
    account_password = Column(String(100))
    account_email = Column(String(100))

    description = Column(String)  # 账号描述
    icon_id = Column(Integer, ForeignKey("icon_files.id"), nullable=True)  # 关联图标文件的ID
    tag = Column(String(50), index=True)  # 用来分类
    create_at = Column(DateTime, default=datetime.now(timezone.utc))  # 记录创建的时间
    update_at = Column(
        DateTime,
        default=datetime.now(timezone.utc),
        onupdate=datetime.now(timezone.utc),
    )  # 记录更新的时间
    remark = Column(String)  # 备注
    
    icon = relationship("DIconFile", lazy="joined")  # 关联图标文件
    
class DIconFile(Base):
    __tablename__ = "icon_files"
    id = Column(Integer, primary_key=True, index=True)

    file_name = Column(String(100), nullable=False)
    file_url = Column(String(255), nullable=False)
    file_size = Column(Integer, nullable=False)  # 文件大小，以字节为单位
    file_type = Column(String(50), nullable=False)  # 文件类型（如：png, jpg等）
    create_at = Column(DateTime, default=datetime.now(timezone.utc))
    update_at = Column(
        DateTime,
        default=datetime.now(timezone.utc),
        onupdate=datetime.now(timezone.utc),
    )